<?php
namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
/*
   首页列表
*/
class IndexCenterController extends Controller{
    /*
        公告展示
    */
    public function ShowIndex($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $page = isset($pos['page']) ? $pos['page']  : 1;
        $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
        $offset = ($page - 1) * $pageSize;
        if(!CheckController::CheckParameter($pos,['UserId','temp'])){//
           return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            // print_r($UserPurview);die();
            if($UserPurview == 'All' || $UserPurview == 'Personal'){
                if($pos['temp'] == 1){//我的任务
                    $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('m.mission_id as MissionId','c.case_id as CaseId','c.case_title as CaseTitle','c.case_create_time as CaseCreateTime','c.case_executor_id as CaseExecutorId','c.case_member_id as CaseMemberId','c.case_from_id as CaseFromId','m.case_id as CaseId','m.mission_content as MissionContent','m.mission_count as MissionCount','m.mission_complete as MissionComplete','m.mission_end as MissionEnd')
                                                   ->join("yl_mission as m",function($join){
                                                        $join->on('c.case_id','=','m.case_id');
                                                    })
                                                    ->whereRaw("FIND_IN_SET($pos[UserId],case_member_id) or FIND_IN_SET($pos[UserId],case_from_id) or FIND_IN_SET($pos[UserId],case_executor_id)");
                        $count =$CaseInfo->count();
                        $CaseInfo=$CaseInfo->offset($offset)->limit($pageSize)
                        ->get()->map(function ($val){
                            $val = (array)$val;
                            return $val;
                        })
                        ->toArray();
                        $sum_page = ceil($count/$pageSize);  //总页码
                        if($page > $sum_page && $sum_page > 0 ){
                            return RequestBackController::Structure(301);
                        }else{
                            if($CaseInfo){
                                $result['list'] = $this->ListData($CaseInfo); 
                                $result['count'] = $count;
                                return RequestBackController::Structure(501,$result);
                            }else{
                                return RequestBackController::Structure(504);
                            }
                                
                        }
                }
                if($pos['temp'] == 2){//待我审批
                    $approval = DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_id as ApprovalModeId','m.approval_assessor_id as ApprovalAssessorId','a.approval_mode_id as ApprovalModeId','a.case_id as CaseId','a.approval_content as ApprovalContent','a.approval_created_time as ApprovalCreatedTime','a.approval_id as ApprovalId','c.case_id as CaseId','c.case_title as CaseTitle','c.case_create_time as CaseCreateTime','c.case_executor_id as CaseExecutorId','c.case_member_id as CaseMemberId','c.case_from_id as CaseFromId')
                                ->join("yl_case as c",function($join){
                                                        $join->on('c.case_id','=','a.case_id');
                                                    })
                                ->join("yl_approval_mode as m",function($join){
                                                        $join->on('a.approval_mode_id','=','m.approval_mode_id');
                                                    })
                                ->whereRaw("FIND_IN_SET($pos[UserId],m.approval_assessor_id)");
                                $count =$approval->count();
                                $approval=$approval->offset($offset)->limit($pageSize)
                                ->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                })
                                ->toArray();
                                // print_r($approval);die();
                        $sum_page = ceil($count/$pageSize);  //总页码
                        if($page > $sum_page && $sum_page > 0 ){
                            return RequestBackController::Structure(301);
                        }else{
                            if($approval){
                                $result['list'] = $this->ListData1($approval); 
                                $result['count'] = $count;
                                return RequestBackController::Structure(501,$result);
                            }else{
                                return RequestBackController::Structure(504);
                            }
                                
                        }
                }
                if($pos['temp'] == 3){//发起审批
                    $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('a.approval_mode_id as ApprovalModeId','c.case_id as CaseId','c.case_title as CaseTitle','c.case_create_time as CaseCreateTime','c.case_executor_id as CaseExecutorId','c.case_member_id as CaseMemberId','c.case_from_id as CaseFromId','m.case_id as CaseId','m.mission_content as MissionContent','m.mission_count as MissionCount','m.mission_complete as MissionComplete','m.mission_end as MissionEnd','a.case_id as CaseId','a.approval_creater_id as ApprovalCreaterId','a.approval_content as ApprovalContent')
                                                   ->join("yl_mission as m",function($join){
                                                        $join->on('c.case_id','=','m.case_id');
                                                    })
                                                   ->join("yl_approval as a",function($join){
                                                        $join->on('c.case_id','=','a.case_id');
                                                    })
                                                    ->where('a.approval_creater_id','=',$pos['UserId']);
                        $count =$CaseInfo->count();
                        $CaseInfo=$CaseInfo->offset($offset)->limit($pageSize)
                        ->get()->map(function ($val){
                            $val = (array)$val;
                            return $val;
                        })
                        ->toArray();
                        $sum_page = ceil($count/$pageSize);  //总页码
                        if($page > $sum_page && $sum_page > 0 ){
                            return RequestBackController::Structure(301);
                        }else{
							//print_r($CaseInfo);die;
                            if($CaseInfo){
                                $result['list'] = $this->ListData1($CaseInfo); 
                                $result['count'] = $count;
                                return RequestBackController::Structure(501,$result);
                            }else{
                                return RequestBackController::Structure(504);
                            }
                                
                        }

                }
                if($pos['temp'] == 4){//超时任务
                    $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('m.mission_id as MissionId','c.case_id as CaseId','c.case_title as CaseTitle','c.case_create_time as CaseCreateTime','c.case_executor_id as CaseExecutorId','c.case_member_id as CaseMemberId','c.case_from_id as CaseFromId','m.case_id as CaseId','m.mission_content as MissionContent','m.mission_count as MissionCount','m.mission_complete as MissionComplete','m.mission_end as MissionEnd','a.case_id as CaseId','a.approval_creater_id as ApprovalCreaterId','a.approval_content as ApprovalContent')
                                                   ->join("yl_mission as m",function($join){
                                                        $join->on('c.case_id','=','m.case_id');
                                                    })
                                                   ->join("yl_approval as a",function($join){
                                                        $join->on('c.case_id','=','a.case_id');
                                                    });
                        $count =$CaseInfo->count();
                        $CaseInfo=$CaseInfo->offset($offset)->limit($pageSize)
                        ->get()->map(function ($val){
                            $val = (array)$val;
                            return $val;
                        })
                        ->toArray();
                        $sum_page = ceil($count/$pageSize);  //总页码
                        if($page > $sum_page && $sum_page > 0 ){
                            return RequestBackController::Structure(301);
                        }else{
                            if($CaseInfo){
								$_task=[];
								//print_r($CaseInfo);die;
								foreach ($CaseInfo as $k => $v) {
                                    $_temp = json_decode($CaseInfo[$k]['MissionContent'],true);
									//print_r($_temp);die;
									if(!empty($_temp))
									foreach($_temp as $k1=>$v1)
									{
										foreach($v1['task'] as $tk=>$tv)
										{
											//print_r($v1['task']);die;
											if(!$tv['state'])
											{
												//print_r($tv['state']);die;
												$_time=strtotime($tv['content']['date']);
												if($_time <time()&&$_time<>'')
												{
													$_task[]=$CaseInfo[$k];
												}
											}
										}
									}
                                }
                                //print_r($_task);die();
                               // $result['list'] = isset($_task)?$this->ListData($_task):''; 
							   if(!empty($_task)){
								   $result['list'] = $this->ListData($_task); 
							   }else{
								   $result['list'] = '';
							   }
							   $result['count'] = count($_task);
                                return RequestBackController::Structure(501,$result);
                            }else{
                                return RequestBackController::Structure(504);
                            }
                                
                        }

                }
				if($pos['temp'] == 5){//即将过期（日期还剩2天则即将过期）
                    $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('m.mission_id as MissionId','c.case_id as CaseId','c.case_title as CaseTitle','c.case_create_time as CaseCreateTime','c.case_executor_id as CaseExecutorId','c.case_member_id as CaseMemberId','c.case_from_id as CaseFromId','m.case_id as CaseId','m.mission_content as MissionContent','m.mission_count as MissionCount','m.mission_complete as MissionComplete','m.mission_end as MissionEnd','a.case_id as CaseId','a.approval_creater_id as ApprovalCreaterId','a.approval_content as ApprovalContent')
                                                   ->join("yl_mission as m",function($join){
                                                        $join->on('c.case_id','=','m.case_id');
                                                    })
                                                   ->join("yl_approval as a",function($join){
                                                        $join->on('c.case_id','=','a.case_id');
                                                    });
                                                    
                        $count =$CaseInfo->count();
                        $CaseInfo=$CaseInfo->offset($offset)->limit($pageSize)
                        ->get()->map(function ($val){
                            $val = (array)$val;
                            return $val;
                        })
                        ->toArray();
                        $sum_page = ceil($count/$pageSize);  //总页码
                        if($page > $sum_page && $sum_page > 0 ){
                            return RequestBackController::Structure(301);
                        }else{
							
                            if($CaseInfo){
                                $task=[];
                                 foreach ($CaseInfo as $k => $v) {
                                    $_temp = json_decode($CaseInfo[$k]['MissionContent'],true);
                                    if(!empty($_temp))
                                    foreach($_temp as $k1=>$v1)
                                    {
                                        foreach($v1['task'] as $tk=>$tv)
                                        {	
                                            if(!$tv['state'])
                                            {
												//print_r(strtotime($tv['content']['date']));die;
                                                $_time=strtotime($tv['content']['date']);
												
                                                if($_time<>''){
                                                    $day = $_time - strtotime(date('Y-m-d',time()));
                                                    if($day == 172800){
                                                        $task[]=$CaseInfo[$k];
                                                    }
                                                    
                                                }
                                            }
                                        }
                                    }
                                }	
								//print_r($task);die;
								if(!empty($task)){
								   $result['list'] = $this->ListData($task); 
							   }else{
								   $result['list'] = '';
							   }
							   $result['count'] = count($task);
							   return RequestBackController::Structure(501,$result);
								
                                
                            }else{
                                return RequestBackController::Structure(504);
                            }
                                
                        }
                }
            }
       if($UserPurview == 'None'){//暂无任何权限
           return RequestBackController::Structure(403);
       }
    }
}


  protected function ListData($info){
            $UserInfo = DB::connection('mysql_other')->table('yl_user as u')->select('u.user_id as UserId','u.user_name as UserName','u.user_login as UserLogin','u.user_img as UserImg')
                ->get()->map(function ($val){
                    $val = (array)$val;
                    return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['UserId'];
                    })
                    ->toArray();
						//print_r($ApprovalMode);die;
            foreach ($info as $k => $v) {
                $info[$k]['CaseExecutorId']=explode(',',$v['CaseExecutorId']);
                foreach ($info[$k]['CaseExecutorId'] as $k1=>$v1) {
                    $info[$k]['CaseExecutorId'][$k1] = $UserInfo[$v1];
                }
                $info[$k]['CaseCreateTime'] =date('Y-m-d H:s',$v['CaseCreateTime']);
				//$info[$k]['ApprovalModeId'] = isset($ApprovalMode[$info[$k]['ApprovalModeId']]['ApprovalModeTitle'])?$ApprovalMode[$info[$k]['ApprovalModeId']]['ApprovalModeTitle']:'';
            }
            if($info){
                return $info;
            }else{
                return false;
            }

    }
	protected function ListData1($info){
            $UserInfo = DB::connection('mysql_other')->table('yl_user as u')->select('u.user_id as UserId','u.user_name as UserName','u.user_login as UserLogin','u.user_img as UserImg')
                ->get()->map(function ($val){
                    $val = (array)$val;
                    return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['UserId'];
                    })
                    ->toArray();
			$ApprovalMode = DB::connection('mysql_other')->table('yl_approval_mode')->select('approval_mode_id as ApprovalModeId','approval_mode_title as ApprovalModeTitle')->get()->map(function ($val){
                            $val = (array)$val;
                            return $val;
                        })
                        ->keyBy(function($item)
                        {
                            return $item['ApprovalModeId'];
                        })
                        ->toArray();
						//print_r($ApprovalMode);die;
            foreach ($info as $k => $v) {
                $info[$k]['CaseExecutorId']=explode(',',$v['CaseExecutorId']);
                foreach ($info[$k]['CaseExecutorId'] as $k1=>$v1) {
                    $info[$k]['CaseExecutorId'][$k1] = $UserInfo[$v1];
                }
                $info[$k]['CaseCreateTime'] =date('Y-m-d H:s',$v['CaseCreateTime']);
				$info[$k]['ApprovalModeTitle'] = isset($ApprovalMode[$info[$k]['ApprovalModeId']]['ApprovalModeTitle'])?$ApprovalMode[$info[$k]['ApprovalModeId']]['ApprovalModeTitle']:'';
            }
            if($info){
                return $info;
            }else{
                return false;
            }

    }

  









   
}